
/*
    461. 汉明距离
    优化：使用移位，先将两个数进行异或处理，
    再将处理后的数最后一位与1进行比较，比较完成后将数右移一位，
    循环上述过程，直到数为0
 */

public class HammingDistance {

    public static void main(String[] args) {
        int x = 2;
        int y = 1;
        System.out.println(hammingDistance(x, y));
    }

    public static int hammingDistance(int x, int y) {
        String s1 = Integer.toBinaryString(x);
        String s2 = Integer.toBinaryString(y);
        if (s1.length() > s2.length()) return hammingDistance(y, x);
        int lenght = s2.length() - s1.length();
        while (lenght > 0) {
            s1 = "0" + s1;
            lenght--;
        }
        int count = 0;
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)) count++;
        }
        return count;
    }
}
